#!/bin/tcsh -ef
#############################################################################
#                                                                           #
# Title: Get Spacegroup & PhaseOrigin                                       #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 03/01/2006                                             #
# Last Modification: 03/01/2006                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 45
#
# MANUAL: This script runs the MRC program <I>allspace</I> (Valpuesta et al.), which calculates the internal phase residuals for all space groups. This script then chooses the optimal space group and the corresponding phase origin and returns these to the 2dx_image database as entries for <B>Symmetry</B> and <B>Phase Origin</B>.
#
# MANUAL: You can restrict the spacegroups to test. If you are sure that the crystal is of a square space group, it is for example useless to test for p3 symmetry.  
#
# MANUAL: You can also use this script to find the phase origin for an image, if you already know the space group --in this case put the switch <i>Test other spacegroups</I> to <I>No</I>.
#
# MANUAL: Note that only projection images of non-tilted crystals show symmetry. A tilted view of for example a p3 crystal is usually no longer symmetric.
#
# PUBLICATION: <A HREF="http://www.2dx.unibas.ch/documentation/mrc-software/mrc-programs/allspace">Allspace documentation</A>
#
# RESET: test_spacegroups_doit = "y"
# RESET: test_spacegroups_val = "ALL"
#
# DISPLAY: RESMIN
# DISPLAY: RESMAX
# DISPLAY: SYM
# DISPLAY: phaori
# DISPLAY: phaoriFouFilter
# DISPLAY: beamtilt
# DISPLAY: realcell
# DISPLAY: realang
# DISPLAY: rot180
# DISPLAY: revhk
# DISPLAY: rot90
# DISPLAY: revhnd
# DISPLAY: revxsgn
# DISPLAY: comment
# DISPLAY: allspace_step
# DISPLAY: allspace_stepnum
# DISPLAY: allspace_IQMAX
# DISPLAY: allspace_RESMAX
# DISPLAY: movie_enable
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
set imagename = ""
set defocus = ""
set realang = ""
set realcell = ""
set RESMIN = ""
set RESMAX = ""
set CS = ""
set KV = ""
set SYM = ""
set rot180 = ""
set revhk = ""
set rot90 = ""
set revhnd = ""
set revxsgn = ""
set realcell = ""
set realang = ""
set phaori = ""
set phaoriFouFilter = ""
set beamtilt = ""
set allspace_step = ""
set allspace_stepnum = ""
set allspace_IQMAX = ""
set allspace_RESMAX = ""
set movie_enable = ""
set test_spacegroups_doit = ""
set test_spacegroups_val = ""
#
#$end_vars
#
set scriptname = 2dx_allspace
#
\rm -f LOGS/${scriptname}.results
#
echo "<<@evaluate>>"
#
set IS_2DX = yes
source ${proc_2dx}/initialize
#
echo bin_2dx = ${bin_2dx}
echo proc_2dx = ${proc_2dx}
#
set iname = ${imagename}
@
echo "<<@progress: 20>>"
#
# Translate SYM into spcgroup_num:
source ${proc_2dx}/2dx_sym2spcgrp_sub.com
#
if ( ${test_spacegroups_doit} == "n" || ${test_spacegroups_doit} == "N" ) then
  set test_spacegroups_val = "SPEC"
  echo "::Finding only phase origin in symmetry ${SYM}."
else
  echo "::Symmetries to test = ${test_spacegroups_val}"
endif
#
echo "::Stepsize and Phase Search Array Size = ${allspace_step} , ${allspace_stepnum}"
echo "::IQ Max = ${allspace_IQMAX},  Resolution Max = ${allspace_RESMAX}"
#
\rm -f SCRATCH/${scriptname}-SPCGRP.txt SCRATCH/${scriptname}-OUTFILE.txt
#
set allspace_warn = `echo ${allspace_step} ${allspace_stepnum} | awk '{ if ( $1 * ( $2 - 1 ) != 360 ) { s = 1 } else { s = 0 }} END { print s }'`
if ( ${allspace_warn} == '1' ) then
  ${proc_2dx}/linblock "+"
  ${proc_2dx}/linblock "WARNING: Number of steps times stepsize is not equal 360 degrees."
  echo "WARNING: Number of steps times stepsize is not equal 360 degrees." >> LOGS/${scriptname}.results
  set allspace_betterstep = `echo ${allspace_stepnum} | awk '{ s = 360.0 / ( $1 - 1 ) } END { print s }'`
  ${proc_2dx}/linblock "Better use a step size of ${allspace_betterstep}"
  ${proc_2dx}/linblock "+"
endif
#
set allspace_warn = `echo ${allspace_RESMAX} ${RESMAX} | awk '{ if ( $1 < $2 ) { s = 1 } else { s = 0 }} END { print s }'`
if ( ${allspace_warn} == '1' ) then
  ${proc_2dx}/linblock "#"
  set allspace_RESMAX = ${RESMAX}
  echo "set allspace_RESMAX = ${allspace_RESMAX}" >> LOGS/${scriptname}.results
  ${proc_2dx}/linblock "WARNING: Resolution was higher than data resolution. Now corrected."
  echo "WARNING: Resolution was higher than data resolution. Now corrected." >> LOGS/${scriptname}.results
  ${proc_2dx}/linblock "#"
endif  
#
set aphfile = APH/image_ctfcor_fou_unbent_ctf.aph
#
setenv IN ${aphfile}
#
echo "# IMAGE-IMPORTANT: ${aphfile} <APH: Input Amp&Phs File>" >> LOGS/${scriptname}.results
#
if ( ${rot180} == 'y' ) then
  set rot180val = '1'
else
  set rot180val = '0'
endif
#
if ( ${revhk} == 'y' ) then
  set revhkval = '1'
else
  set revhkval = '0'
endif
#
if ( ${rot90} == 'y' ) then
  set rot90val = '1'
else
  set rot90val = '0'
endif
#
if ( ${revhnd} == 'y' ) then
  set revhndval = '1'
else
  set revhndval = '0'
endif
#
if ( ${revxsgn} == 'y' ) then
  set revxsgnval = '1'
else
  set revxsgnval = '0'
endif
#
#
echo "<<@progress: 40>>"
#
if ( ${test_spacegroups_val} == "SPEC" ) then
  #
  ${bin_2dx}/2dx_allspacea.exe << eot 
${test_spacegroups_val}
${spcgrp}
T T F 4000				! SEARCH,REFINE,TILT,NCYC
${phaori} ${beamtilt}			! ORIGH,ORIGK,TILTH,TILTK
${allspace_step},${allspace_stepnum} 	! STEPSIZE, PHASE SEARCH ARRAY SIZE
${realcell} ${realang} ${RESMIN} ${allspace_RESMAX} ${CS} ${KV} ! A, B, GAMMA, RIN, ROUT, CS, KV
F ${rot180val} ${allspace_IQMAX} ${revhkval} ${rot90val} ${revhndval} ${revxsgnval}	! ILIST,ROT180,IQMAX,REVHK,ROT90,REVHND,REVXSGN
"SCRATCH/${scriptname}-SPCGRP.txt"
"SCRATCH/${scriptname}-OUTFILE.txt"
eot
  #
else
  #
  ${bin_2dx}/2dx_allspacea.exe << eot 
${test_spacegroups_val}
T T F 4000                              ! SEARCH,REFINE,TILT,NCYC
${phaori} ${beamtilt}                             ! ORIGH,ORIGK,TILTH,TILTK
${allspace_step},${allspace_stepnum}    ! STEPSIZE, PHASE SEARCH ARRAY SIZE
${realcell} ${realang} ${RESMIN} ${allspace_RESMAX} ${CS} ${KV} ! A, B, GAMMA, RIN, ROUT, CS, KV
F ${rot180val} ${allspace_IQMAX} ${revhkval} ${rot90val} ${revhndval} ${revxsgnval}	! ILIST,ROT180,IQMAX,REVHK,ROT90,REVHND,REVXSGN
"SCRATCH/${scriptname}-SPCGRP.txt"
"SCRATCH/${scriptname}-OUTFILE.txt"
eot
  #
endif
#
cat SCRATCH/${scriptname}-OUTFILE.txt
set SPCGRPTXT = `cat SCRATCH/${scriptname}-SPCGRP.txt | cut -f1`
set spcgrp = `echo ${SPCGRPTXT} | cut -d\  -f2`
set PHAORIX = `echo ${SPCGRPTXT} | cut -d\  -f7`
set PHAORIY = `echo ${SPCGRPTXT} | cut -d\  -f8`
set phaori_local = `echo ${PHAORIX},${PHAORIY}`
${proc_2dx}/linblock "Best SpaceGroup is ${spcgrp}"
${proc_2dx}/lin "with: ${SPCGRPTXT}"
#
echo "<<@progress: 80>>"
#
# FouFilter Unbending result is used:
echo "set phaoriFouFilter = ${phaori_local}" >> LOGS/${scriptname}.results
echo "set phaori = ${phaori_local}" >> LOGS/${scriptname}.results
#
if ( ${test_spacegroups_val} != "SPEC" ) then
  #
  echo "set SYM = ${spcgrp}" >> LOGS/${scriptname}.results
  #
  if ( ${spcgrp} == 'p3' || ${spcgrp} == 'p312' ) then
    if ( ${realang} != "120.0" ) then
      set realang = 120.0
      echo "set realang = ${realang}" >> LOGS/${scriptname}.results
    endif
  endif
  #
  if ( ${spcgrp} == 'p6' || ${spcgrp} == 'p622' ) then
    if ( ${realang} != "120.0" ) then
      set realang = 120.0
      echo "set realang = ${realang}" >> LOGS/${scriptname}.results
    endif
  endif
  #
  if ( ${spcgrp} == 'p4' || ${spcgrp} == 'p422' || ${spcgrp} == 'p4212' ) then
    if ( ${realang} != "90.0" ) then
      set realang = 90.0
      echo "set realang = ${realang}" >> LOGS/${scriptname}.results
    endif
  endif
  #
endif
#
if ( ${spcgrp} == 'p2221b' ) then
  ${proc_2dx}/linblock "WARNING: Conflict with CCP4 axis definition in p2221b"
  ${proc_2dx}/linblock "The standard setting in CCP4 has the 2fold screw along Z."
  ${proc_2dx}/linblock "This is not yet implemented. Please issue a bug report to 2dx.org"
endif
#
if ( ${spcgrp} == 'p12_a' || ${spcgrp} == 'p121_a' || ${spcgrp} == 'c12_a' || ${spcgrp} == 'p2221a' ) then
  echo ":: "
  echo ":: "
  ${proc_2dx}/linblock "ERROR: This symmetry ${spcgrp} is not supported by CCP4."
  echo ":: "
  ${proc_2dx}/linblock "You should reindex 90-deg rotated."
  ${proc_2dx}/linblock "This can be done with the specific script EVALUATE LATTICE."
  ${proc_2dx}/linblock "The run everything from GET SPOTLIST again."
  echo ":: "
endif
#
echo "set PHASEORI_done = y" >> LOGS/${scriptname}.results
#
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
#
echo "<<@progress: 100>>"
#


